<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: 16 条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small" @click="showDialog = true">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table border :data="list" :default-sort="{ prop: 'timeOfEntry' }">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式">
            <template v-slot="scope">
              {{ formatEmployment(scope.row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable />
          <el-table-column label="操作" width="280">
            <template v-slot="scope">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="del(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination layout="total, sizes,prev, pager, next, jumper" :total="total" :page-size="params.size"
            :page-sizes="[2, 5, 10]" @current-change="hCurrentChange" @size-change="hSizeChange" />
        </el-row>
      </el-card>
    </div>
    <el-dialog title="新增员工" :visible.sync="showDialog">
      <add @close="showDialog = false" />
    </el-dialog>
  </div>
</template>

<script>
import add from "./empDialog.vue";
import { getEmployeeList, delEmployee } from '@/api/employees'
import EmployeeEnum from '@/constant/employees'
const hireType = EmployeeEnum.hireType.reduce((acc, item) => { return { ...acc, [item.id]: item.value } }, {})
console.log('枚举信息', EmployeeEnum.hireType, hireType)

export default {
  name: 'Employees',
  components: {
    add
  },
  data() {
    return {
      showDialog: false,
      list: [],
      params: {
        page: 1, // 当前页码
        size: 5 // 每页几条
      },
      total: 0 // 总共数据条数
    }
  },
  created() {
    this.loadEmployeeList()
  },
  methods: {
    async loadEmployeeList() {
      try {
        const res = await getEmployeeList(this.params)
        this.list = res.data.rows
        this.total = res.data.total
      } catch (err) {
        console.log(err)
        this.$message.error('加载员工数据失败')
      }
    },
    // 聘用形式转化
    formatEmployment(id) {
      // 从hireType中 通过id找到对应的元素的value
      // const item = hireType.find(it => it.id === num)
      // if (item) {
      //   return item.value
      // } else {
      //   return '不能确定'
      // }
      // return  ? '正式' : '非正式'
      return hireType[id] || '未知'
    },
    // 会自动接收当前点击的页码
    hCurrentChange(curPage) {
      // alert(curPage)
      // 1. 更新页码
      this.params.page = curPage
      // 2. 重发请求
      this.loadEmployeeList()
    },
    // 每页几条
    hSizeChange(curSize) {
      // alert(size)
      // 1. 更新每页的条数
      this.params.size = curSize
      // 2. 重发请求
      this.loadEmployeeList()
    },
    // 删除
    del(id) {
      console.log(id)
      this.$confirm('确认删除吗？').then(() => {
        this.doDel(id)
      }).catch(() => { })
    },
    async doDel(id) {
      await delEmployee(id)
      // 如果删除第最后一页（例如4）的最一条数据之后，页面会显示不正常
      if (this.employees.length === 1) {
        this.params.page--
        if (this.params.page <= 0) {
          this.params.page = 1
        }
      }
      this.loadEmployeeList()
    }
  }
}
</script>
